from rich.console import Console
from rich.table import Table
from rich.layout import Layout
from rich.panel import Panel
from rich.live import Live
import random
import time

console = Console()

def data_comparison():
    layout = Layout()
    layout.split_column(
        Layout(name="left", ratio=1),
        Layout(name="right", ratio=1)
    )
    
    # 左侧面板
    left_layout = Layout()
    left_layout.split_row(
        Layout(Panel("原始数据", height=10), name="raw"),
        Layout(Panel("处理后数据", height=10), name="processed")
    )
    layout["left"].update(left_layout)
    
    # 右侧面板
    right_layout = Layout()
    right_layout.split_row(
        Layout(name="diff"),
        Layout(Panel("操作日志", height=10), name="log")
    )
    layout["right"].update(right_layout)
    
    with Live(layout, refresh_per_second=2) as live:
        while True:
            # 模拟数据更新
            raw_data = [random.randint(1,100) for _ in range(5)]
            processed = [x*2 for x in raw_data]
            
            # 更新左侧表格
            raw_table = Table(title="原始数据")
            raw_table.add_column("索引")
            raw_table.add_column("值")
            for i, v in enumerate(raw_data):
                raw_table.add_row(str(i), str(v))
            left_layout["raw"].update(raw_table)
            
            # 更新右侧表格
            diff_table = Table(title="差异对比")
            diff_table.add_column("索引")
            diff_table.add_column("原始值")
            diff_table.add_column("处理值")
            diff_table.add_column("差异")
            for i in range(5):
                diff_table.add_row(str(i), str(raw_data[i]), str(processed[i]), str(processed[i]-raw_data[i]))
            right_layout["diff"].update(diff_table)
            
            # 模拟日志更新
            right_layout["log"].update(Panel(
                "[green]数据处理完成[/green] 时间: " + time.strftime("%H:%M:%S")
            ))

data_comparison()